using System;
using System.Web.Mvc;
using Complaint.Models;
using Complaint.Models.Dtos;

namespace Complaint.Areas.Admin.Controllers
{
    public class CategoryFormViewModel
    {
        public Category Category { get; private set; }
        public SelectList ParentList { get; private set; }

        public CategoryFormViewModel(Category category)
        {
            Category = category;
            using (new UnitOfWorkScope(true))
            {
                var m = new CategoryModel();
                var categories = m.Retrieve();
                categories.Insert(0, new Category { CategoryID = 0, Title = "None" });
                ParentList = new SelectList(categories, "CategoryID", "Title");
            }
        }
    }

    public class CategoryController : AdminBaseController
    {
        //
        // GET: /Category/

        public ActionResult Index()
        {
            using (new UnitOfWorkScope(true))
            {
                var model = new CategoryModel();
                return View(model.GetCategoryHierarchy(null));
            }
        }

        //
        // GET: /Category/Create

        public ActionResult Add()
        {
            var category = new Category();
            return PartialView("CategoryForm", new CategoryFormViewModel(category));
        }

        //
        // POST: /Category/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Add(Category category, int parentID)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    using (new UnitOfWorkScope(true))
                    {
                        var model = new CategoryModel();
                        var parent = model.FindById(parentID);
                        category.Parent = parent;
                        model.Create(category);

                        return parentID == 0 
                            ? (ActionResult)PartialView("List", model.GetCategoryHierarchy(null)) 
                            : Json(new  {
                                             id = category.CategoryID,
                                             title = category.Title,
                                             status = category.Status
                                         });
                    }
                }
                catch (Exception e)
                {
                    throw;
                }
            }

            return View(new Category());
        }

        //
        // POST: /Category/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Category category)
        {
            try
            {
                using (new UnitOfWorkScope(true))
                {
                    var model = new CategoryModel();
                    var c = model.FindById(category.CategoryID);
                    c.Title = category.Title;
                    c.Status = category.Status;
                    model.Update(c);
                }
                return Content("Success");
            }
            catch
            {
                return Content("Failed");
            }
        }

        /// <summary>
        /// Delete a leaf category, clientside validation dependency
        /// </summary>
        /// <param name="id">Id of a leaf category to delete</param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id)
        {
            try
            {
                using (new UnitOfWorkScope(true))
                {
                    var model = new CategoryModel();
                    var c = model.FindById(id);

                    model.Delete(c);
                }
                return Content("Success");
            }
            catch
            {
                // error handling here
                return Content("Failed to delete due to an error.");
            }
        }

    }
}